package com.databias.j15.perfmon;

import java.awt.Graphics2D;

import com.databias.j15.GScreen;

import com.xs0.libs.j15.J15;
import com.xs0.libs.j15.J15ButtonListener;
import com.xs0.libs.j15.J15Connection;
import com.xs0.libs.j15.J15Exception;
import com.xs0.libs.j15.J15Screen;

/**
 * This application uses the XS0 interface routines and widgets from databias
 * to create a performance monitor displayed on the G15 keyboard.
 * 
 * The metrics for this are retrieved from the NSClient service using the
 * nsclient4j interface class.
 * 
 * This main class need not change, as the components and their metrics are 
 * stored in the PerfComponents class.  This class simply sets up the screen(s),
 * then periodically calls the metrics helper.  Changes can be made there.
 * 
 * @author Frederick Farzanegan
 *
 */
public class PerformanceMonitor {
	
	boolean stop = false;

	final boolean DEBUG = false; // Should it be displayed on monitor
	final boolean G15 = true; // Should it be displayed on KB


	/**
	 * Create the collection objects, populate them, then update them as appropriate
	 *
	 */
	private void startDemo()
	{
		
		GScreen panelscreen, gscreen=null;
		J15Screen jscreen=null;
		
		// JFrame for debugging
		G15DebugScreen frame=null;

		// Create the screen collection object.
		gscreen = new GScreen();
		panelscreen= new GScreen();

		
		if (DEBUG) frame= new G15DebugScreen("Performance Monitor", panelscreen);

		if (G15)
		{
			try {
				J15Connection conn = J15.get().connect("PerfMon", false, false);

				// Create and attach to our screen
				jscreen= conn.attach(0);
				jscreen.addButtonListener(new J15ButtonListener() {
					public void onButtonChange(J15Screen source, int newButtonState)
					{
						System.out.println("Button:"+(newButtonState)+ " pressed.");
						if (newButtonState==8)
							stop = true;
					}});

				jscreen.updateImage(); // do this to force it to front
				jscreen.setAsForegroundApp(true);
				gscreen.setScreen(jscreen.getGraphics());
				
				
			}
			catch (Exception e) {
				System.out.println(e.getStackTrace());
			}

		} // G15

		

	// Get our widgets created and placed.
		PerfComponents pc = new PerfComponents("localhost");
		
		// Place them on our screens
		pc.addToScreen(gscreen);
		pc.addToScreen(panelscreen);
	

	// Main loop
		while (!stop)
		{
			// Ask the components for an update
			pc.updateComponents();

			// Have the collections update their attached Graphics
			gscreen.draw();
			panelscreen.draw();

			if (DEBUG) frame.repaint();

			if (G15)
				try {
					jscreen.updateImage();
				} catch (J15Exception e1) {
					e1.printStackTrace();
					stop=true;
				}

				// Wait and do it again
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					stop=true;
					e.printStackTrace();
				}
		}  // Main loop


		if (G15) J15.cleanUp(); // Static call to the screen manager
		System.exit(0);

	}

	
	public static void main(String[] args) throws J15Exception
	{
		PerformanceMonitor d = new PerformanceMonitor();
		d.startDemo();
	}
}